//
//  PHDiscoverView.swift
//  PHWeibo
//
//  Created by 潘荟 on 16/7/22.
//  Copyright © 2016年 BAT. All rights reserved.
//

import UIKit

class PHDiscoverView: UIView {

    override init(frame: CGRect) {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREENW, height: 35))
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 设置界面
    func setupUI() {
        
//        self.backgroundColor = UIColor.greenColor()
        //添加子控件
        addSubview(cancelButton)
        addSubview(searchTextField)
        
        //设置约束
        cancelButton.snp_makeConstraints { (make) -> Void in
            make.width.equalTo(50)
            make.centerY.equalTo(self)
            make.right.equalTo(self)
        }
        
        searchTextField.snp_makeConstraints { (make) -> Void in
            make.top.leading.bottom.equalTo(self)
            make.trailing.equalTo(self)
        }
        
    }
    // MARK: - 添加监听事件
    func cancelClick() {
        
        cancelButton.hidden = true
        searchTextField.snp_updateConstraints {(make) -> Void in
            make.trailing.equalTo(self)
        }
        
        UIView.animateWithDuration(0.3) { () -> Void in
            self.layoutIfNeeded()
            self.searchTextField.resignFirstResponder()
        }
    }
    
    
    // MARK: - 懒加载子控件
    
    private lazy var cancelButton:UIButton = {
       let cancelBtn = UIButton()
        
        cancelBtn.addTarget(self, action: "cancelClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        cancelBtn.setTitle("取消", forState: UIControlState.Normal)
        cancelBtn.setTitleColor(UIColor.lightGrayColor(), forState: UIControlState.Normal)
        cancelBtn.hidden = true
        
        return cancelBtn
    }()
    
    private lazy var searchTextField:UITextField = {
        let searchTxt = UITextField()
        
        
        
//        searchTxt.layer.borderColor = UIColor.grayColor().CGColor
//        searchTxt.layer.borderWidth = 2
        searchTxt.borderStyle = .RoundedRect
        searchTxt.backgroundColor = UIColor.lightGrayColor()
        
        searchTxt.placeholder = "大家正在搜:海贼王"
        
        let img: UIImageView = UIImageView(imgName: "searchbar_textfield_search_icon")
        
        searchTxt.leftView = img
        searchTxt.leftViewMode = .Always
        img.contentMode = .Center
        img.frame.size = CGSize(width: 35, height: 35)
        
        searchTxt.delegate = self
        return searchTxt
    }()

}

// MARK: -textField代理方法

extension PHDiscoverView:UITextFieldDelegate {
    func textFieldDidBeginEditing(textField: UITextField) {
        cancelButton.hidden = false
        
        searchTextField.snp_updateConstraints { (make) -> Void in
            make.trailing.equalTo(self).offset(-50)
        }
        
        UIView.animateWithDuration(0.3) { () -> Void in
            self.layoutIfNeeded()
        }
    }
}










